package com.adingxiong.quickstart.project.controller;


import com.adingxiong.quickstart.project.entity.Files;
import com.adingxiong.quickstart.project.entity.vo.FileVo;
import com.adingxiong.quickstart.project.service.IFilesService;
import com.adingxiong.quickstart.project.service.IGoodsInfoService;
import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 固件更新信息 前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/ware")
@Api(tags = "固件更新记录")
public class FilesController {

    @Autowired
    private IFilesService filesService;

    @Autowired
    private IGoodsInfoService goodsInfoService;

    @PostMapping("/save")
    @ApiOperation(value = "保存固件更新的信息")
    public Result saveInfo(@RequestBody Files files){
        if(files == null){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        files.setCreateTime(LocalDateTime.now());
        boolean insert = filesService.save(files);
        return new Result(insert ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),insert ? "新增成功" : "新增失败",insert);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改固件更新的信息")
    public Result updateInfo( @RequestBody Files aboutus){
        if(aboutus == null){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        boolean update = filesService.updateById(aboutus);
        return new Result(update ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),update ? "修改成功" : "修改失败",update);
    }

    @GetMapping("/del")
    @ApiOperation(value = "删除多条")
    @Transactional
    public Result dels(
            @ApiParam(value = "id数组" ,required = true)  @RequestParam(value = "ids") List<Integer> ids){
        if(ids == null || ids.size() == 0){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        boolean del = filesService.removeByIds(ids);
        return new Result(del ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),del ? "删除成功" : "删除失败",del);
    }

    @ApiOperation(value = "高级查询")
    @GetMapping("/list")
    public Result<Page<Files>> getList(
            @ApiParam(value = "版本号") @RequestParam(value = "version" ,required = false) String version,
            @ApiParam(value = "当前页码") @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            @ApiParam(value = "每页数量") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ){
        QueryWrapper<Files> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(version)) {
            queryWrapper.eq("version" ,version);
        }
        Page page = new Page();
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        Page<Files> res = filesService.page(page,queryWrapper);
        Page<FileVo> result = new Page<>();
        List<Files> files = res.getRecords();
        List<FileVo> list = new ArrayList<>();
        if(files != null && files.size()>0){
            List<Integer> ids = files.stream().map(Files::getGoodsId).collect(Collectors.toList());
            HashMap<Integer,String> names = goodsInfoService.findNameByIds(ids);
            for(Files vo : files){
                FileVo fileVo = new FileVo(vo);
                fileVo.setGoodsName(names.get(vo.getGoodsId()));
                list.add(fileVo);
            }
            result.setRecords(list);
            result.setTotal(res.getTotal());
            result.setCurrent(currentPage);
            result.setPages(pageSize);

            return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),result);
        }
        return new Result<>(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),res);
    }

}

